package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    //提供常量
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";


    //获取session对象,由Spring MVC提供的工具类
    public static HttpSession getSession() {
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //把用户/权限信息存在session中
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(USER_IN_SESSION,employee);
    }
    public static void setPermissions(List<String> list){
        getSession().setAttribute(PERMISSION_IN_SESSION,list);
    }

    //从session获取用户/权限
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    public static List<String> getPermissions(){
        return (List<String>)getSession().getAttribute(PERMISSION_IN_SESSION);
    }
}
